import { remove, getList, save } from '@/api/house/community'
import {DatePicker} from "element-ui";
import Li_area_select from "../../../components/area/li_area_select";
import permission from '@/directive/permission/index.js'

export default {

  components: {
    Li_area_select
  },

  data() {
    return {
      formVisible: false,
      formTitle: '添加社区',
      isAdd: true,
      form: {
        name:'',
        provinceCode:'',
        provinceName:'',
        cityCode:'',
        cityName:'',
        areaCode:'',
        areaName:'',
        street:'',
        hasFile:0,
        id: '',
        createBy: '',
        createTime: ''

      },
      listQuery: {
        page: 1,
        limit: 20,
        id: undefined
      },
      total: 0,
      list: null,
      listLoading: true,
      selRow: {},

      address:{
        pcode:'',
        province: '',
        ccode:'',
        city: '',
        acode:'',
        area: '',
      },
    }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  computed: {

    //表单验证
    rules() {
      return {
        provinceCode: [
          { required: true, message: '请选择省', trigger: 'blur' }
        ],
        cityCode: [
          { required: true, message: '请选择市', trigger: 'blur' }
        ],
        areaCode: [
          { required: true, message: '请选择区', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入社区名称', trigger: 'blur' },
          { min: 2, max: 64, message: '长度在 2 到 64 个字符', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.fetchData();
    },
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.records
        this.listLoading = false
        this.total = response.data.total
      })
    },
    search() {
      this.fetchData()
    },
    reset() {
      this.listQuery.id = ''
      this.fetchData()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleClose() {

    },
    fetchNext() {
      this.listQuery.page = this.listQuery.page + 1
      this.fetchData()
    },
    fetchPrev() {
      this.listQuery.page = this.listQuery.page - 1
      this.fetchData()
    },
    fetchPage(page) {
      this.listQuery.page = page
      this.fetchData()
    },
    changeSize(limit) {
      this.listQuery.limit = limit
      this.fetchData()
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow
    },
    resetForm() {
      this.form = {
        name:'',
        provinceCode:'',
        provinceName:'',
        cityCode:'',
        cityName:'',
        areaCode:'',
        areaName:'',
        street:'',
        hasFile:0,
        id: ''
      },
      this.address = {
        pcode:'',
        province: '',
        ccode:'',
        city: '',
        acode:'',
        area: '',
      }
    },
    add() {
      this.resetForm()
      this.formTitle = '添加社区',
      this.formVisible = true
      this.isAdd = true
    },
    validAddress() {
      if (this.address.pcode == '') {
        return -1;
      }else if(this.address.ccode == ''){
        return -2;
      }else if(this.address.acode == ''){
        return -3;
      }
      return 0;
    },
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          let r = this.validAddress();
          if (r == 0) {
            save({
              name:this.form.name,
              provinceCode:this.address.pcode,
              provinceName:this.address.province,
              cityCode:this.address.ccode,
              cityName:this.address.city,
              areaCode:this.address.acode,
              areaName:this.address.area,
              street:this.form.street,
              hasFile:this.form.hasFile,
              id: this.form.id,
              createBy: this.form.createBy,
              createTime: this.form.createTime
            }).then(response => {
              this.$message({
                message: this.$t('common.optionSuccess'),
                type: 'success'
              })
              this.fetchData()
              this.formVisible = false
            })
          }else{
            if(r == -1){
              this.$message({
                message: '省份不能为空',
                type: 'error'
              })
            }else if(r == -2){
              this.$message({
                message: '市区不能为空',
                type: 'error'
              })
            }else if(r == -3){
              this.$message({
                message: '县不能为空',
                type: 'error'
              })
            }else{
              this.$message({
                message: '数据填写错误',
                type: 'error'
              })
            }
          }

        } else {
          return false
        }
      })
    },
    checkSel() {
      if (this.selRow && this.selRow.id) {
        return true
      }
      this.$message({
        message: this.$t('common.mustSelectOne'),
        type: 'warning'
      })
      return false
    },
    edit() {
      if (this.checkSel()) {
        this.isAdd = false
        this.form = this.selRow
        this.formTitle = '编辑社区'
        this.formVisible = true
        this.address.pcode = this.form.provinceCode
        this.address.province = this.form.provinceName
        this.address.ccode = this.form.cityCode
        this.address.city = this.form.cityName
        this.address.acode = this.form.areaCode
        this.address.area = this.form.areaName
      }
    },
    remove() {
      if (this.checkSel()) {
        var id = this.selRow.id
        this.$confirm(this.$t('common.deleteConfirm'), this.$t('common.tooltip'), {
          confirmButtonText: this.$t('button.submit'),
          cancelButtonText: this.$t('button.cancel'),
          type: 'warning'
        }).then(() => {
          remove(id).then(response => {
            this.$message({
              message: this.$t('common.optionSuccess'),
              type: 'success'
            })
            this.fetchData()
          }).catch( err=> {
            this.$notify.error({
              title: '错误',
              message: err
            })
          })
        }).catch(() => {
        })
      }
    },

  },


}
